import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;

import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';


class Utils {
  static LatLng calcZoom(List<LatLng> geoCoordinateList) {
    int total = geoCoordinateList.length;
    double X = 0, Y = 0, Z = 0;
    for (LatLng g in geoCoordinateList) {
      double lat, lon, x, y, z;
      lat = g.latitude * math.pi / 180;
      lon = g.longitude * math.pi / 180;
      x = math.cos(lat) * math.cos(lon);
      y = math.cos(lat) * math.sin(lon);
      z = math.sin(lat);
      X += x;
      Y += y;
      Z += z;
    }
    X = X / total;
    Y = Y / total;
    Z = Z / total;
    double Lon = math.atan2(Y, X);
    double Hyp = math.sqrt(X * X + Y * Y);
    double Lat = math.atan2(Z, Hyp);
    return LatLng(Lat * 180 / math.pi, Lon * 180 / math.pi);
  }

  static Future<List> getTracks() async {
    String path = "assets/hotdata/hotlines.json";
    List value = await rootBundle.loadStructuredData(path, (jsonStr) async {
      return json.decode(jsonStr);
    });
    return value;
  }

  static Future<List> getLines() async {
    String path = "assets/hotdata/colds.json";
    List value = await rootBundle.loadStructuredData(path, (jsonStr) async {
      return json.decode(jsonStr);
    });
    return value;
  }

  static Future<List<LatLng>> getTrack(String name, int? type) async {
    final List<LatLng> points = <LatLng>[];
    String path = "assets/${type == 0 ? 'hotdata' : 'tracedata'}/$name";
    String value = await rootBundle.loadStructuredData(path, (jsonStr) async {
      return type == 0 ? jsonStr : (json.decode(jsonStr))['trackLine'];
    });
    final List<String> ll = value.split(' ');
    for (var element in ll) {
      if (element.isNotEmpty) {
        List ls = element.split(',');
        var lat = ls[0];
        lat = double.parse(lat);
        var lng = ls[1];
        lng = double.parse(lng);
        points.add(LatLng(lng, lat));
      }
    }
    return points;
  }

  static final Storage _storage = Storage();
  static Future<List<LatLng>> getMyLine() async {
    String content = await _storage.readCounter();
    final List<LatLng> points = <LatLng>[];
    final List<String> ll = content.split(' ');
    for (var element in ll) {
      if (element.isNotEmpty) {
        List ls = element.split(',');
        var lat = ls[0];
        var lng = ls[1];
        lat = double.parse(lat);
        lng = double.parse(lng);
        points.add(LatLng(lng, lat));
      }
    }
    return points;
  }


}

class Storage {
  Future<String> get _localPath async {
    final _path = await getTemporaryDirectory();
    return _path.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/counter.txt');
  }

  Future<String> getPath() async{
    String path =  await _localPath;
    path = '$path/counter.txt';
    return path;
  }

  void clearData() async{
    final file = await _localFile;
    file.deleteSync();
  }

  Future<String> readCounter() async {
    try {
      final file = await _localFile;
      var contents = await file.readAsString();
      return contents;
    } catch (e) {
      print(e);
      return '';
    }
  }

  Future<File> writeCounter(String counter,{FileMode mode = FileMode.writeOnlyAppend}) async {
    final file = await _localFile;
    if(file.existsSync() && file.lengthSync() >0) {
      counter = ',' + counter;
    }
    return file.writeAsString(counter,mode: mode);
  }
}
